//
//  XLCommitDataCell.m
//  XiangLove
//
//  Created by 宋振 on 2018/8/7.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLCommitDataCell.h"
#import "XLPersonCenterTool.h"
#import "XLRecordTool.h"
#import <AVFoundation/AVFoundation.h>

@interface XLCommitDataCell()

//标题
@property (nonatomic, strong) UILabel *  titleLbl;

@end

@implementation XLCommitDataCell
{
    //语音动画容器
    UIImageView *autoImageView;
    
    //动画是否正在执行
    BOOL isSpeech;
    
    NSString *playUrl;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        
        /**
         设置界面显示
         */
        [self initUI];
        
    }
    
    return self;
}

#pragma mark    - - - 设置界面显示 - - -
- (void)initUI
{
    /**
     设置标题
     */
    [self addSubview:self.titleLbl];
    
    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.left.equalTo(self.mas_left).offset(kScale(15));
        make.right.lessThanOrEqualTo(self.mas_centerX);
    }];
    
    /**
     设置指向图标
     */
    UIImageView *arrowImg = [[UIImageView alloc] init];
    
    arrowImg.image = [[UIImage imageNamed:@"xl_arrow_icon"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    
    [self addSubview:arrowImg];
    
    [arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-kScale(7));
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    /**
     设置描述信息
     */
    [self addSubview:self.descripLbl];
    
    [self.descripLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(arrowImg.mas_left);
        make.width.mas_equalTo(kScale(163));
    }];
    
    /**
     语音按钮
     */
    [self addSubview:self.speechButton];
    
    [self.speechButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(arrowImg.mas_left);
        make.size.mas_equalTo(CGSizeMake(60, 28));
    }];
    
    //语音动画容器
    [self autoClickImageView];
}



#pragma mark    -   -   -   数据设置    -   -   -

- (void)setModel:(CommitDataModel *)model
{
    _model = model;
    
    
    //标题
    self.titleLbl.text = K_DEFULT(model.title);
    
    //描述文字
    self.descripLbl.attributedText = [self layOutDescripText:K_DEFULT(model.descrip)];
    
    //语音时间
    
    if (model.isSpeech) {
        
        self.speechButton.hidden = NO;
        self.descripLbl.hidden = YES;
        
        [self.speechButton setAttributedTitle:[self layOutSpeechSecondText:[NSString stringWithFormat:@"%@'",K_DEFULT(model.descrip)]] forState:(UIControlStateNormal)];
        
        if (K_DEFULT(model.recorderUrl).length <= 0) {
            self.speechButton.hidden = YES;
        }
        playUrl = model.recorderUrl;
        
    }else{
        self.speechButton.hidden = YES;
        self.descripLbl.hidden = NO;
    }
    
}




#pragma mark    -   -   - 懒加载 -  -   -

//标题
- (UILabel *)titleLbl
{
    if (!_titleLbl) {
        self.titleLbl = [[UILabel alloc] init];
        self.titleLbl.font = [UIFont fontWithName:kFontNamePFRegular size:17];
        self.titleLbl.textAlignment = NSTextAlignmentLeft;
        self.titleLbl.textColor = RGB(17, 17, 17);
    }
    
    return _titleLbl;
}

//描述文件
- (UILabel *)descripLbl
{
    if (!_descripLbl) {
        self.descripLbl = [[UILabel alloc] init];
        self.descripLbl.textAlignment = NSTextAlignmentRight;
    }
    
    return _descripLbl;
}

//语音按钮
- (UIButton *)speechButton
{
    if (!_speechButton) {
        self.speechButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.speechButton.backgroundColor = RGB(255, 187, 1);
        self.speechButton.frame = CGRectZero;
        [self.speechButton setImage:[UIImage imageNamed:@"common_dx_yuyin3_n"] forState:(UIControlStateNormal)];
        
        [self.speechButton setAttributedTitle:[self layOutSpeechSecondText:K_DEFULT(self.model.descrip)] forState:(UIControlStateNormal)];
        
        self.speechButton.layer.cornerRadius = kScale(14);
        self.speechButton.layer.masksToBounds = YES;
        
        [self.speechButton addTarget:self action:@selector(clickSpeechAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        self.speechButton.hidden = YES;
    }
    
    return _speechButton;
}

#pragma mark    -   -   - Button Action -   -   -

//语音动画
- (void)clickSpeechAction:(UIButton *)sender
{
    
    
    
    //判断当前动画是否正在执行
    if (isSpeech) {
        
        //如果语音没有播放 那么停止动画
        if ([[XLRecordTool shareRecordTool].player isPlaying]) {
            [[XLRecordTool shareRecordTool].player stop];
            
        }
        [self speechStop];
        
        return;
    }
    
    //开始动画
    [self speechStart];
    
    //获取播放地址
    [XLRecordTool shareRecordTool].playUrl = K_DEFULT(playUrl);
    
    
    //播放录音
    [[XLRecordTool shareRecordTool] playRecord];
    
    //录音时长
    NSInteger second = self.model.second.integerValue;
    [XLRecordTool shareRecordTool].playCost = second;
    
    //临时延长时间
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(second * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        //动画结束
        [self speechStop];
    });
}

#pragma mark    - - - Tools(辅助方法) - - -


/**
 语音动画开始 结束 事件
 */
- (void)speechStart
{
    isSpeech = YES;
    [self.speechButton.imageView startAnimating];
    [self.speechButton setImage:[UIImage imageNamed:@"common_dx_yuyin1_n"] forState:(UIControlStateNormal)];
}
- (void)speechStop
{
    isSpeech = NO;
    [self.speechButton.imageView stopAnimating];
    [self.speechButton setImage:[UIImage imageNamed:@"common_dx_yuyin3_n"] forState:(UIControlStateNormal)];
}

/**
 添加语音动画
 */
- (void)autoClickImageView
{
    
    //修饰
    
    //动画集
    NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:@"common_dx_yuyin1_n"],[UIImage imageNamed:@"common_dx_yuyin2_n"],[UIImage imageNamed:@"common_dx_yuyin3_n"], nil];
    
    //设置动画集
    self.speechButton.imageView.animationImages = images;
    
    //时长
    self.speechButton.imageView.animationDuration = 1.0f;
    
    //重复动画
    self.speechButton.imageView.animationRepeatCount = 0;

}



/**
 设置描述字体
 */
- (NSAttributedString *)layOutDescripText:(NSString *)text
{
    NSDictionary *dic = @{NSKernAttributeName:@1,
                          NSForegroundColorAttributeName:RGB(153, 153, 153),
                          NSFontAttributeName:[UIFont fontWithName:KFontNamePFMedium size:15]
                          };
    
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:K_DEFULT(text) attributes:dic];
    
    return attribute;
}

/**
 设置语音读秒字体
 */
- (NSAttributedString *)layOutSpeechSecondText:(NSString *)text
{
    NSDictionary *dic = @{
                          NSKernAttributeName:@1,
                          NSForegroundColorAttributeName:RGB(255, 255, 255),
                          NSFontAttributeName:[UIFont fontWithName:KFontNamePFMedium size:12]
                          };
    
    NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:text attributes:dic];
    
    return attribute;
}


#pragma mark    -   -   - 设置数据信息 -   -   -

- (void)layoutSubviews
{
    [super layoutSubviews];
    
}





@end


@implementation CommitDataModel



@end
